package com.hky.front_end_monitor.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hky.front_end_monitor.entity.SysWebVital;
import com.hky.front_end_monitor.mapper.SysWebVitalMapper;
import com.hky.front_end_monitor.service.SysWebVitalService;
import com.hky.front_end_monitor.utils.StringUtils;
import com.hky.front_end_monitor.utils.UuidGen;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author hello
 * @description 针对表【sys_web_vital】的数据库操作Service实现
 * @createDate 2022-07-28 19:01:13
 */
@Service
public class SysWebVitalServiceImpl extends ServiceImpl<SysWebVitalMapper, SysWebVital>
        implements SysWebVitalService {
    @Resource
    SysFirstPaintServiceImpl fpService;
    @Resource
    SysFirstContentfulPaintServiceImpl fcpService;
    @Resource
    SysLargestContentfulPaintServiceImpl lcpService;
    @Resource
    SysNavigationTimingServiceImpl navigationTimingService;
    @Resource
    SysResourceFlowServiceImpl resourceFlowService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean storeWebVital(Map<String, Object> map) {
        String userId = StringUtils.caseToString(map.get("userId"));
        String[] uuids = UuidGen.getUUID(7);
        assert uuids != null;
        int idx = 0;
        boolean success = true; // 不知道为什么会有警告
        SysWebVital sysWebVital = new SysWebVital();
        sysWebVital.setUserId(userId);
        sysWebVital.setWebVitalId(uuids[idx++]);
        success &= fpService.storeFp(uuids[idx], map);
        sysWebVital.setFirstPaintId(uuids[idx++]);
        success &= fcpService.storeFcp(uuids[idx], map);
        sysWebVital.setFirstContentPaintId(uuids[idx++]);
        success &= navigationTimingService.storeNT(uuids[idx], map);
        sysWebVital.setNavigationTimingId(uuids[idx++]);
        success &= lcpService.storeLcp(uuids[idx], map);
        sysWebVital.setLargestContentfulPaintId(uuids[idx]);

        success &= resourceFlowService.storeRF(uuids[0], map);
        success &= this.save(sysWebVital);
        if (!success) TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return success;
    }
}




